def Prost(p):
    p = int(p)
    k = 2
    while k < p:
        if p % k == 0:
            return False
        else:
            k += 1
    if k == p:
        return True
    
p1 = int(input())
if 1000 <= p1 <= 9999:
    p2 = p1 % 1000
    p3 = p1 // 10
    p4 = p1 // 100
    p5 = p1 % 100
    p6 = p1 % 1000 // 10
    p7 = p1 // 1000
    p8 = p1 % 1000 // 100
    p9 = p1 // 10 % 10
    p10 = p1 % 10
    k = 0
    if Prost(p1) and p1 > k:
        k = p1
    if Prost(p2) and p2 > k:
        k = p2
    if Prost(p3) and p3 > k:
        k = p3
    if Prost(p4) and p4 > k:
        k = p4
    if Prost(p5) and p5 > k:
        k = p5
    if Prost(p6) and p6 > k:
        k = p6
    if Prost(p7) and p7 > k:
        k = p7
    if Prost(p8) and p8 > k:
        k = p8
    if Prost(p9) and p9 > k:
        k = p9
    if Prost(p10) and p10 > k:
        k = p10
    print(k)
else:
    print(0)
